﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Xml.Utility;

namespace Portal.Cms.Model
{
    public class DocSignerCls
    {
        public string DocSignerId;
        public string DocSignerName;
        public string JobTitle;
        public string frkDocReleaseFromId;
        public string frkGroupDataId;
        public int TotalItem = 0;
    }

    public class DocSignerParser
    {
        public static DocSignerCls CreateInstance()
        {
            //tao 1 class trang....
            DocSignerCls oDocSignerCls = new DocSignerCls();
            return oDocSignerCls;
        }

        public static DocSignerCls ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            DocSignerCls oDocSignerCls = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oDocSignerCls;
        }


        public static DocSignerCls ParseFromDataRow(DataRow dr)
        {
            DocSignerCls oDocSignerCls = new DocSignerCls();
            oDocSignerCls.DocSignerId = XmlUtility.GetString(dr, "DocSignerId", true);
            oDocSignerCls.frkDocReleaseFromId = XmlUtility.GetString(dr, "frkDocReleaseFromId", true);
            oDocSignerCls.frkGroupDataId = XmlUtility.GetString(dr, "frkGroupDataId", true);
            oDocSignerCls.DocSignerName = XmlUtility.GetString(dr, "DocSignerName", true);
            oDocSignerCls.JobTitle = XmlUtility.GetString(dr, "JobTitle", true);
            oDocSignerCls.TotalItem = XmlUtility.GetInt(dr, "TotalItem", true);
            return oDocSignerCls;
        }

        public static DocSignerCls[] ParseFromDataTable(DataTable dtTable)
        {
            DocSignerCls[] DocSigners = new DocSignerCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                DocSigners[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return DocSigners;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet();
            ds.Tables.Add("Info");

            ds.Tables["Info"].Columns.Add("DocSignerId");
            ds.Tables["Info"].Columns.Add("frkGroupDataId");
            ds.Tables["Info"].Columns.Add("frkDocReleaseFromId");
            ds.Tables["Info"].Columns.Add("DocSignerName");
            ds.Tables["Info"].Columns.Add("JobTile");
            ds.Tables["Info"].Columns.Add("TotalItem");
            return ds;
        }

        public static string GetXml(DocSignerCls oDocSignerCls)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                oDocSignerCls.DocSignerId,
                oDocSignerCls.frkGroupDataId,
                oDocSignerCls.DocSignerName,
                oDocSignerCls.JobTitle,
                oDocSignerCls.TotalItem,
            });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            DocSignerCls oDocSigner = DocSignerParser.ParseFromDataRow(dr);
            string Xml = DocSignerParser.GetXml(oDocSigner);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            DocSignerCls[] DocSigners = new DocSignerCls[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                DocSigners[iIndex] = DocSignerParser.ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = DocSignerParser.GetXml(DocSigners);
            return Xml;
        }


        public static string GetXml(DocSignerCls[] DocSigners)
        {
            if (DocSigners == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                DocSigners = new DocSignerCls[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < DocSigners.Length; iIndex++)
            {
                DocSignerCls oDocSignerCls = DocSigners[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                {
                    oDocSignerCls.DocSignerId,
                    oDocSignerCls.frkGroupDataId,
                    oDocSignerCls.frkDocReleaseFromId,
                    oDocSignerCls.DocSignerName,
                    oDocSignerCls.JobTitle,
                    oDocSignerCls.TotalItem,
                });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static DocSignerCls[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0)
            {
                return new DocSignerCls[0];
            }
            DocSignerCls[] DocSigners = new DocSignerCls[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                DocSigners[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return DocSigners;
        }
    }
}
